package desktop.lan.remote.util;

import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.Remote;
import java.rmi.RemoteException;
import java.util.logging.Level;
import java.util.logging.Logger;

import desktop.lan.LanInfo;
import desktop.lan.cooperate.LanCooperateClientScene;

/**
 * 
 * @author wuyang
 * @param <I>远程接口
 */
public class RemoteClient<I extends Remote> {
	private I remoteInterface;

	public I getRemoteInterface() {
		return remoteInterface;
	}

	@SuppressWarnings("unchecked")
	public RemoteClient(String remoteName) {
		boolean bound = false;
		while (!bound) {
			try {
				this.remoteInterface = (I) Naming.lookup("rmi://"
						+ LanInfo.getMateIp() + ":" + LanInfo.getMatePort()
						+ "/" + remoteName);
				bound = true;
			} catch (NotBoundException ex) {
				Logger.getLogger(LanCooperateClientScene.class.getName()).log(
						Level.SEVERE, null, ex);
			} catch (MalformedURLException ex) {
				Logger.getLogger(LanCooperateClientScene.class.getName()).log(
						Level.SEVERE, null, ex);
			} catch (RemoteException ex) {
				Logger.getLogger(LanCooperateClientScene.class.getName()).log(
						Level.SEVERE, null, ex);
			}
			System.out.println("try again");
		}
	}
}
